#!/bin/sh
# bootlimn install
PATH=/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/bin:/usr/sbin

# Add a new grub/lilo entry
GRUBBY=/sbin/grubby
if [ -x $GRUBBY ]; then
	kernel=$($GRUBBY --default-kernel)
	initrd=$($GRUBBY --info=$kernel | sed -n '/^initrd=/{s/^initrd=//;p;q;}')
	[ ! -z $initrd ] && initrd="--initrd=$initrd"
	$GRUBBY --remove-kernel TITLE="bootlimn"
	$GRUBBY --copy-default --add-kernel=$kernel $initrd --args="init=/sbin/stpcaller" --title="bootlimn"
	echo "New GRUB entry created"
fi

# creating the dirctory structure for the output

mkdir /bootlimnko		# for the kernel modules 
echo "Created /bootlimnko"
mkdir /bootlimnoutput		# for data
echo "Created /bootlimoutput"

chmod +x /bootlimnoutput
echo "Permission set to /bootlimnoutput"
chmod +x /bootlimnko
echo "Permission set to /bootlimnko"

#copying the stpcaller to sbin

cp ./stpcaller /sbin/stpcaller
if test $? != 0; then
    echo 'Unable to copy stpcaller'
else 
    echo "stpcaller copied"
fi

#compile systemtap probe and copy the respective kernel objects to /bootlimnko

stap -k -g -p4 -m processinfo ./stp/processinfo.stp 
if test $? != 0; then
    echo 'Unable to create processinfo.ko'
else
   echo "processinfo.ko created"
fi
cp ./processinfo.ko /bootlimnko/processinfo.ko

stap -k -g -p4 -m blockioinfo ./stp/blockioinfo.stp
if test $? != 0; then
    echo 'Unable to create blockioinfo.ko'
else
   echo "blockioinfo.ko created"
fi
cp ./blockioinfo.ko /bootlimnko/blockioinfo.ko

stap -k -g -p4 -D MAXMAPENTRIES=10000 -m readwriteinfo ./stp/readwriteinfo.stp
if test $? != 0; then
    echo 'Unable to create readwriteinfo.ko'
else
   echo "readwriteinfo.ko created"
fi
cp ./readwriteinfo.ko /bootlimnko/readwriteinfo.ko

stap -k -g -p4 -m statsinfo ./stp/statsinfo.stp 
if test $? != 0; then
    echo 'Unable to create statsinfo.ko'
else
   echo "statsinfo.ko created"
fi
cp ./statsinfo.ko /bootlimnko/statsinfo.ko

stap -k -g -p4 -m perproinfo ./stp/perproinfo.stp 
if test $? != 0; then
    echo 'Unable to create perproinfo.ko'
else
   echo "perproinfo.ko created"
fi
cp ./perproinfo.ko /bootlimnko/perproinfo.ko

echo "Cleaning Up..."
rm -f ./processinfo.ko
rm -f ./blockioinfo.ko
rm -f ./readwriteinfo.ko
rm -f ./statsinfo.ko
rm -f ./perproinfo.ko
echo "Clean up done"
echo "Install complete! View the README for further instructions"



